/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.controllers.mobileweb;

import app.Const;
import app.dtos.GameCalResult;
import app.dtos.WeekInfo;
import app.interceptors.MobileWxMemberInterceptor;
import app.kits.CalendarKit;
import app.models.game.*;
import app.models.member.Member;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.collect.Lists;
import com.jfinal.aop.Before;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *
 * 游戏目前只有2个：
 *  刮刮乐(id=1)
 *  大转盘(id=2)
 *
 *  这个2个游戏不可以修改，
 *
 *  iｄ是预订义，确定好的，不可以修改，
 *  通过iｄ进行判断处理游戏的接口
 *
 * Created by devil
 * 趣味互动
 */
@Before(MobileWxMemberInterceptor.class)
public class GameController extends BasicController {
    private static final int GAME_ID_GUAGUALE = 1;
    private static final int GAME_ID_DAZHUANPAN = 2;
    private static final int GAME_ID_QIANDAO = 3;

    public static final String GAME_1_NAME = "刮刮乐";
    public static final String GAME_2_NAME = "大转盘";

    public static final String SIGN_NAME = "签到";

    /*private static final String GAME_CUT_INTEGRAL_1 = "刮刮乐";
    private static final String GAME_CUT_INTEGRAL_2 = "大转盘";
    private static final String GAME_CUT_CURRENCY_1 = "刮刮乐";
    private static final String GAME_CUT_CURRENCY_2 = "大转盘";


    private static final String SIGN_ADD_CURRENCY = "签到";
    private static final String SIGN_ADD_INTEGRAL = "签到";*/

    /*private static final int SIGN_INTEGRAL_BASE = 5;
    private static final int SIGN_INTEGRAL_MAX = 20;*/

    /**
     * 游戏列表
     */
    public void index() {

        SignRule signRule = SignRule.dao.findFirst(SqlKit.sql("signrule.findByStatus"), true);
        setAttr("signRule", signRule);

        List<Game> games = Game.dao.gameList();
        setAttr("games", games);
        render("game.ftl");
    }

    @Before(Tx.class)
    public void sign() {
        int gameId = getParaToInt("gameid", 0);
        SignRule signRule = SignRule.dao.findFirst(SqlKit.sql("signrule.findByStatus"), true);
        if (signRule == null) {
            renderJson(AjaxMessage.error("没有签到活动"));
            return;
        }

        boolean getIntegral = signRule.getBoolean("get_integral");
        double baseIntegral = signRule.getDouble("base_integral") != null ? signRule.getDouble("base_integral") : 0;
        double incrIntegral = signRule.getDouble("incr_integral")  != null ? signRule.getDouble("incr_integral") : 0;
        double maxIntegral = signRule.getDouble("max_integral") != null ? signRule.getDouble("max_integral") : 0;

        boolean getCurrency = signRule.getBoolean("get_currency");
        double baseCurrency = signRule.getDouble("base_currency") != null ? signRule.getDouble("base_currency") : 0;
        double incrCurrency = signRule.getDouble("incr_currency") != null ? signRule.getDouble("incr_currency") : 0;
        double maxCurrency = signRule.getDouble("max_currency") != null ? signRule.getDouble("max_currency") : 0;

        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);

        Member member = Member.dao.findById(mid);
        if (member == null) {
            renderJson(AjaxMessage.error("没有该会员"));
            return;
        }

        //今天
        Date now = new Date();
        String today = DateFormatUtils.format(now, DateKit.dateFormat);
        //昨天
        Date yest = CalendarKit.getYesterday();
        String before = DateFormatUtils.format(yest, DateKit.dateFormat);
        //查询会员的签到统计
        SignStat stat = SignStat.dao.findByMemberId(mid);

        int contTimes = -1;
        double integral = -1;
        double currency = -1;

        boolean isNew = false;

        if (stat == null) { //从来没有签到过
            stat = new SignStat();
            isNew = true;
            if (getIntegral) { //有积分增加
                integral = baseIntegral;
            }
            if (getCurrency) { //有货币增加
                currency = baseCurrency;
            }
            contTimes = 1;
        } else { //已经签到过
            Date last_date = stat.getDate("last_date");
            String last_date_format = DateFormatUtils.format(last_date, DateKit.dateFormat);

            if (last_date_format.equals(today)) { //今天已经签到过
                renderJson(AjaxMessage.ok("今日已经签到"));
                return;
            } else {
                if (last_date_format.equals(before)) { //昨天签到过，表示签到连续
                    int cont_times = stat.getInt("cont_times");
                    if (getIntegral) {//有积分增加
                        double lastIntegral = stat.getDouble("last_integral") != null ? stat.getDouble("last_integral") : 0;
                        if (lastIntegral >= maxIntegral) { //积分到最大值，积分不增加
                            integral = maxIntegral;
                        } else { //不到最大值，增加积分
                            integral = lastIntegral + incrIntegral;
                        }
                    }
                    if (getCurrency) { //有货币增加
                        double lastCurrency = stat.getDouble("last_currency") != null ? stat.getDouble("last_currency") : 0;
                        if (lastCurrency >= maxCurrency) { //积分到最大值，积分不增加
                            currency = maxCurrency;
                        } else { //不到最大值，增加积分
                            currency = lastCurrency + incrCurrency;
                        }
                    }
                    contTimes = cont_times + 1;
                } else { //昨天没有签到，重新开始计算
                    if (getIntegral) { //有积分增加
                        integral = baseIntegral;
                    }
                    if (getCurrency) { //有货币增加
                        currency = baseCurrency;
                    }
                    contTimes = 1;
                }
            }
        }

        SignLog log = new SignLog();
        log.set("member", mid);
        log.set("sign_date", now);
        log.set("sign_time", now);

        stat.set("member", mid);
        stat.set("last_date", now);
        if (integral > -1) {
            stat.set("last_integral", integral);
            log.set("integral", integral);
        }
        if (currency > -1) {
            stat.set("last_currency", currency);
            log.set("currency", currency);
        }

        stat.set("cont_times", contTimes);
        if (isNew) {
            stat.save();
        } else {
            stat.update();
        }

        log.save();

        if (integral > -1) {
            Member.dao.optIntegral(mid, true, integral, SIGN_NAME);
        }
        if (currency > -1) {
            Member.dao.optCurrency(mid, true, currency, SIGN_NAME);
        }

        renderJson(AjaxMessage.ok("签到成功"));
    }

    public void showgame3() {
        //查询游戏
        int gameId = getParaToInt("gameid", 0);
        SignRule game = SignRule.dao.findById(gameId);
        setAttr("game", game);
        setAttr("gid", gameId);
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);

        Member member = Member.dao.findById(mid);
        if (member == null) {
            String wxno = getPara(Const.WX_PARAM);
            redirect("/mobileweb/game?mid="+ mid +"&wxno="+wxno);
            return;
        }

        Calendar todayCal = Calendar.getInstance();
        Date todayDate = todayCal.getTime();
        String todayDayYMD = DateFormatUtils.format(todayDate, DateKit.dateFormat);
        int todayWeek = todayCal.get(Calendar.DAY_OF_WEEK);
        String todayWeekCn = WeekInfo.WEEK_CNS[todayWeek-1];
        WeekInfo today = new WeekInfo();
        today.dayYmd = todayDayYMD;
        today.week = todayWeek;
        today.weekCn = todayWeekCn;
        setAttr("today", today);

        List<WeekInfo> weekInfos = Lists.newArrayList();
        Date date = null;
        int weeksize = 7;
        CalendarKit tt = new CalendarKit();
        for (int i=0;i<weeksize;i++) {
            if (date == null) {
                date = tt.getMonday();
            } else {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.DATE, 1);
                date = cal.getTime();
            }
            Calendar c = Calendar.getInstance();
            c.setTime(date);

            String dayYmd = DateFormatUtils.format(date, DateKit.dateFormat);
            int week = c.get(Calendar.DAY_OF_WEEK);
            String weekCn = WeekInfo.WEEK_CNS[week-1];

            WeekInfo weekInfo = new WeekInfo();
            weekInfo.dayYmd = dayYmd;
            weekInfo.week = week;
            weekInfo.weekCn = weekCn;
            weekInfos.add(weekInfo);
        }

        long weekCount = SignLog.dao.balCount(weekInfos.get(0), weekInfos.get(weekInfos.size() - 1), mid);

        SignLog.dao.processWeekInfos(weekInfos, today, mid);

        setAttr("weekInfos", weekInfos);
        setAttr("weekCount", weekCount);
        render("showgame3.ftl");
    }

    /**
     * 游戏1详情，刮刮乐
     */
    public void showgame1() {
        if (setGameInfo()) {
            render("showgame1.ftl");
        }
    }

    /**
     * 游戏2详情，大转盘
     */
    public void showgame2() {
        if (setGameInfo()) {
            render("showgame2.ftl");
        }
    }

    private boolean setGameInfo() {
        //查询游戏
        int gameId = getParaToInt("gameid", 0);
        Game game = Game.dao.findById(gameId);
        //查询游戏规则
        int gameRuleId = game.getInt("game_rule");
        GameRule gameRule = GameRule.dao.findById(gameRuleId);
        //查询游戏奖项
        //List<GameAwards> gameAwardses = GameAwards.dao.findByGameId(gameId);
        List<Record> records = GameAwards.dao.findWithGiftByGameId(gameId);
        //查询某个会员当天进行某个游戏的次数
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        Member member = Member.dao.findById(mid);


        if (member == null) {
            String wxno = getPara(Const.WX_PARAM);
            redirect("/mobileweb/game?mid="+ mid +"&wxno="+wxno);
            return false;
        }

        String today = DateFormatUtils.format(new Date(), DateKit.dateFormat);
        int todayPlayTimes = GameJoinrecord.dao.findDayPlayTimes(gameId,mid,today);


        //判断游戏是否还能进行。包括游戏是否已经过了结束时间，
        //游戏中奖人数已经没有名额，用户是否积分不够，用户是否虚拟货币不够
        setAttr("member", member);
        setAttr("game", game);
        setAttr("gameRule", gameRule);
        setAttr("gameAwardses", records);
        setAttr("todayPlayTimes", todayPlayTimes);
        return true;
    }

    /**
     * 感觉这里还有不少问题，不同奖品不同的数量，
     *
     * 判断用户是否中奖，由服务端来统一判断
     * 这里对数据的一致性要求非常高，不能出现告诉用户中奖了可是奖品又没了的情况
     * 同一个时刻，只能有一个用户在进行抽奖，这样处理最稳妥
     */
    public void getUserWin() {
        int gameId = 0;
        String param = getPara("gameid");
        if (param == null || param == "") {
            gameId = int.class.cast(param);
        }

        boolean isWin = isUserWin();

        renderJson();
    }

    private synchronized boolean isUserWin() {

        return true;
    }

    /**
     * 游戏记录
     */
    public void gamerecords(){
        int userId = 0;
        String param = getPara("userId");
        List<GameJoinrecord> records = GameJoinrecord.dao.find("select ol_game_awards.name,ol_game_joinrecord.game,ol_game_joinrecord.join_time from ol_game_awards ,ol_game_joinrecord where ol_game_joinrecord.awards=ol_game_awards.id and member=", param);
    }

//==============游戏处理============================
    /**
     * 中奖处理
     */
    public void dispose() {
        try {
            int gid = getParaToInt("gid", 0);
            if (gid == 1) { //刮刮乐
                disposeGame(gid);
            } else if (gid == 2) { //大转盘
                disposeGame(gid);
            } else {
                renderJson(AjaxMessage.error("对不起，没有该游戏设置。"));
            }
        } catch (Exception e) { //没有中奖
            e.printStackTrace();
            renderJson(AjaxMessage.error("对不起，请求出错"));
        }
    }

    /**
     * 游戏处理
     * 1、判断会员是否异常 *
     * 2、判断游戏是否过期（目前从规则中查询是否过期），过期提示未中奖，游戏过期 *
     * 3、判断会员是否已经操作每日免费次数，（进入页面之前已经判断过，这里为了保证安全和后面计算账户扣减）
     * 4、查询会员账户信息，判断是否有足够的积分、中南币；进行积分、中南币等扣减
     * 5、计算游戏中奖与否
     *
     * @param gid
     */
    @Before(Tx.class)
    private synchronized void disposeGame(int gid) {
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l); //会员id
        String wxno = getPara(Const.WX_PARAM); //微信no
        if (mid != 0) {//
            Member member = Member.dao.findById(mid);

            if (member != null) { //有会员信息
                /*String phone = member.getStr("phone");
                if (Strings.isNullOrEmpty(phone)) { //没有手机号码，判断没有绑定会员
                    renderJson(AjaxMessage.error("对不起，尚未绑定会员，无法参与游戏。"));
                } else { //绑定过会员，判定游戏规则*/
                    GameRule gameRule = GameRule.dao.findByGameId(gid);

                    Date now = new Date();
                    Timestamp beginTime = gameRule.getTimestamp("begin_time");
                    Timestamp endTime = gameRule.getTimestamp("end_time");
                    if (beginTime.before(now) && endTime.after(now)) { //开始时间在当前时间之前并且结束时间在当前时间之后
                        //查询当天玩的次数
                        String today = DateFormatUtils.format(now, DateKit.dateFormat);
                        int todayPlayTimes = GameJoinrecord.dao.findDayPlayTimes(gid,mid,today);
                        int freeTimes = gameRule.getInt("free_times");
                        if (freeTimes>todayPlayTimes) { //免费次数大于已玩次数，不扣积分和中南币之类
                            //TODO 计算中奖与否
                            GameCalResult ga = gameArithmetic(gid, gameRule);
                            //TODO 生成游戏记录
                            saveJoinGameRecord(gid, mid, wxno, ga, now);
                            if (ga != null) {
                                renderJson(AjaxMessage.ok(ga));
                            } else {
                                renderJson(AjaxMessage.failure("很遗憾，你没有中奖。"));
                            }

                        } else { //在需要扣减积分、中南币的时候，默认优先使用积分
                            boolean lessIntegral = false;
                            boolean lessCurrency = false;

                            boolean consume = gameRule.getBoolean("consume"); //是否消耗积分

                            if (consume) { //判断是否可以使用积分
                                double integral = member.getDouble("integral"); //当前积分
                                double useIntegral = gameRule.getDouble("integral"); //使用积分
                                if (integral >= useIntegral) { //当前积分满足使用积分
                                    final String game_cut_integral = (gid == GAME_ID_GUAGUALE ? GAME_1_NAME :
                                            gid == GAME_ID_DAZHUANPAN ? GAME_2_NAME : "");
                                    //TODO 扣减积分账户数据
                                    Member.dao.optIntegral(mid, false, useIntegral, game_cut_integral);
                                    //TODO 计算中奖与否
                                    GameCalResult ga = gameArithmetic(gid, gameRule);

                                    //TODO 生成游戏记录
                                    saveJoinGameRecord(gid, mid, wxno, ga, now);
                                    if (ga != null) {
                                        renderJson(AjaxMessage.ok(ga));
                                    } else {
                                        renderJson(AjaxMessage.failure("很遗憾，你没有中奖。"));
                                    }
                                    return;
                                } else {
                                    lessIntegral = true;
                                }
                            }

                            boolean useVirtualCurrency = gameRule.getBoolean("use_virtual_currency");
                            if (useVirtualCurrency) {//判断是否可以使用中南币
                                double currency = member.getDouble("currency"); //当前中南币
                                double virtualCurrency = gameRule.getDouble("virtual_currency"); //使用中南币
                                if (currency>=virtualCurrency) {
                                    final String game_cut_currency = (gid == GAME_ID_GUAGUALE ? GAME_1_NAME :
                                            gid == GAME_ID_DAZHUANPAN ? GAME_2_NAME : "");
                                    //TODO 扣减中南币账户数据
                                    Member.dao.optCurrency(mid, false, virtualCurrency, game_cut_currency);
                                    //TODO 计算中奖与否
                                    GameCalResult ga = gameArithmetic(gid, gameRule);

                                    //TODO 生成游戏记录
                                    saveJoinGameRecord(gid, mid, wxno, ga, now);
                                    if (ga != null) {
                                        renderJson(AjaxMessage.ok(ga));
                                    } else {
                                        renderJson(AjaxMessage.failure("很遗憾，你没有中奖。"));
                                    }
                                    return;
                                } else {
                                    lessCurrency = true;
                                }
                            }

                            //积分、中南部账户不够的情况反馈信息
                            if (consume && useVirtualCurrency) { //可以使用积分和中南币
                                if (lessIntegral && lessCurrency) {
                                    renderJson(AjaxMessage.error("对不起，您的积分，中南币不够玩本次游戏"));
                                }
                            } else {
                                if(lessIntegral){ //尝试使用过积分，积分不够
                                    renderJson(AjaxMessage.error("对不起，您的积分不够玩本次游戏"));
                                } else if(lessCurrency) {//尝试使用过中南币，积分不够
                                    renderJson(AjaxMessage.error("对不起，您的中南币不够玩本次游戏"));
                                } else {//计算错误
                                    renderJson(AjaxMessage.error("对不起，您已经达到免费次数。"));
                                }
                            }
                        }
                    } else {
                        renderJson(AjaxMessage.error("对不起，该游戏已经结束。"));
                    }
               // }
            } else {
                renderJson(AjaxMessage.error("对不起，没有该会员。"));
            }
        } else {
            renderJson(AjaxMessage.error("对不起，没有该会员。"));
        }
    }

    /**
     *
     * 游戏算法
     *  1、计算中奖概率，根据中奖概率计算是否中奖
     *      1.1 设置中奖概率为 10(最大100)(winning) / 100(最大100)(total)，表示参与10次有中奖1次
     *       生成 中奖(winningNumbers) 数组( [5, 4, 10, 111] / [7] / [8])
     *        生成要求：
     *          a). winningNumbers中数字不重复
     *          c). winningNumbers数字随机产生
     *          d). totalNumbers是0-total的数字集合
     *      1.2 随机生成一个0-total的数字，如果这个随机数在winningNumbers中，则为中奖，否则为不中奖
     *
     *  2、在中奖概率计算中奖的情况下（步骤1满足），判断中奖的奖品
     *      2.1 查询还有剩余的数量的所有奖项
     *      2.2 中奖的奖品为 一等奖10名 二等奖30名 三等奖60名，即n1=10, n2=30, n3=60；n=n1+n2+n3=100
     *      2.2 计算出
     *        一等奖中奖区间 n1Start = 0, n1End = n1(10);
     *        二等奖中奖区间 n2Start = n1End, n2End = n2Start + n2;
     *        三等奖中奖区间 n3Start = n2End, n3End = n3Start + n3;
     *      2.3 在0-n中产生随机数num，到中奖区间中去匹配，获取到中奖奖品 num在[n1Start, n1End)/[n2Start, n2End)/[n3Start, n3End)
     *
     *  //3废弃
     *  3、在产生中奖产品前提下，判断中奖奖品是否已经领取完
     *      3.1 获取到giftId（奖品id）和 giftSize（中奖大小）和 gameId
     *      3.2 从游戏记录表中查询，当前游戏(gameId)的当前礼品(giftId)中奖数
     *      3.3 与giftSize进行比较，如果不大与giftSize，则发奖品，最终判定为中奖
     *      3.4 如果在3.3中判断当前这个奖品已经发玩了，则让发下一个
     *
     * @param gid 游戏id
     * @param gameRule 游戏规则
     * @return 返回null 表示没有中中奖， 不返回null表示中奖
     */
    public synchronized GameCalResult gameArithmetic(int gid, GameRule gameRule) {
        int winning = gameRule.getInt("winning");
        int total = gameRule.getInt("total");
        Random random = new Random();

        int[] winningNumbers = winningNumbers(winning, total);
        int num = random.nextInt(total);
        boolean calNumWinning = false;
        for (int i=0, l=winningNumbers.length;i<l;i++) {
            if (num == winningNumbers[i]) {
                calNumWinning = true;
            }
        }

        if (calNumWinning) { //根据概率计算中奖了
            //查询有中奖名额的游戏奖项
            List<Record> records = GameAwards.dao.findBalSizeWithGiftByGameId(gid);

            if (records == null || records.isEmpty()) { //没有查询到奖项
                return null;
            }

            int recordsSize = records.size();
            int[][] recordsNumbers = new int[recordsSize][2]; //{ {nStart, nEnd} }
            int nStart = 0;
            int nEnd = 0;
            for (int i=0;i<recordsSize;i++) {
                int size = records.get(i).getInt("awardsSize");
                nEnd = nStart + size;
                recordsNumbers[i][0] = nStart;
                recordsNumbers[i][1] = nEnd;
                nStart = nEnd;
            }

            int recordsNumber = random.nextInt(nEnd);
            int recordsIndex = -1;
            for (int i=0, index=recordsNumbers.length;i<index;i++) {
                int s = recordsNumbers[i][0];
                int e = recordsNumbers[i][1];
                if (recordsNumber>=s && recordsNumber<e) {
                    recordsIndex = i;
                    break;
                }
            }

            Record winingGameAwards = records.get(recordsIndex);
            if (winingGameAwards != null) { //产生奖项，判断奖项是否还有
                int awardsId = winingGameAwards.getInt("awardsId");
                String awardsName = winingGameAwards.getStr("awardsName");
                int giftId = winingGameAwards.getInt("giftId");
                String giftName = winingGameAwards.getStr("giftName");
                int giftStyle = winingGameAwards.getInt("giftStyle");
                double giftValue = winingGameAwards.getDouble("giftValue") != null ? winingGameAwards.getDouble("giftValue") : 0;

                GameCalResult ag = new GameCalResult();
                ag.awardId = awardsId;
                ag.awardName = awardsName;
                ag.giftId = giftId;
                ag.giftName = giftName;
                ag.giftStyle = giftStyle;
                ag.giftValue = giftValue;

                return ag;
            } else {
                return null;
            }
            /*for (int[] is : recordsNumbers) {
                System.out.println(Arrays.toString(is));
            }
            System.out.println(nEnd);*/
        } else {
            return null;
        }
    }

    /**
     *
     * @param winning
     * @param total
     * @return
     */
    private int[] winningNumbers(int winning, int total){
        int[] winningNumbers = new int[winning];
        if (winning == total) { //中奖数和参与数相等，100%中奖
            for (int i=0;i<winning;i++) {
                winningNumbers[i] = i;
            }
        } else {
            if (total / winning > 4) {
                int i = 0;
                Random random = new Random();
                while (i < winning) {
                    int num = random.nextInt(total);
                    boolean inArr = false;
                    for (int j=0;j<winning;j++) {
                        if(num == winningNumbers[j]) {
                            inArr = true;
                        }
                    }
                    if (!inArr) {
                        winningNumbers[i++] = num;
                    }
                }
            } else {
                for (int i=0;i<winning;i++) {
                    winningNumbers[i] = i;
                }
            }
        }
        return winningNumbers;
    }

    public synchronized void saveJoinGameRecord(int gid, long mid, String wxno, GameCalResult ga, Date now) {
        int i = (int)(Math.random()*100000+100000);
        GameJoinrecord r = new GameJoinrecord();
        r.set("game", gid);
        r.set("member", mid);
        r.set("member_wx", wxno);
        r.set("join_time", now);
        r.set("join_date", now);
        if (ga != null) {
            r.set("winning", true);
            r.set("awards", ga.awardId);
            r.set("gift", ga.giftId);
            r.set("gift_name", ga.giftName);



            //TODO 判断奖项类型，操作账户
            checkGiftStyleAndOperAccount(gid, mid, ga);
            if (ga.accept == 1) { //已经兑换过奖了
                r.set("sms_verify_code", "直接到帐");
                r.set("receive", true);
                r.set("receive_time", now);
            } else {
                r.set("sms_verify_code", i);
                r.set("receive", false);
            }

            ga.sncode = i+"";
        } else {
            r.set("winning", false);
        }


        r.save();

    }

    /**
     * 判断游戏类型并且操作
     */
    public synchronized void checkGiftStyleAndOperAccount(int gid, long mid, GameCalResult ga) {
        if (ga.giftStyle != Const.GIFT_STYLE_GIFT) { //不是礼品，直接操作账户
            double value = ga.giftValue;
            String r = (gid == 1 ? GAME_1_NAME+"中奖" : gid == 2 ? GAME_2_NAME+"中奖" : "");

            if (ga.giftStyle == Const.GIFT_STYLE_CURRENCY) { //中南币
                Member.dao.optCurrency(mid, true, value, r);
            } else if (ga.giftStyle == Const.GIFT_STYLE_INTEGRAL) { //积分
                Member.dao.optIntegral(mid, true, value, r);
            }
            ga.accept = 1;
        }
    }
}
